<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">




<title>Spring.net快速上手指南 - 迷失路灯 - 博客园</title>
<meta name="keywords" content="Spring.net快速上手指南">



<link title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/i-miss-you/rss">
<link title="RSD" type="application/rsd+xml" rel="EditURI" href="http://www.cnblogs.com/i-miss-you/rsd.xml"> 
  



<link rel="stylesheet" type="text/css" href="index.css" media="all">
</head>
<body>
    <form name="Form1" method="post" action="http://www.cnblogs.com/i-miss-you/articles/1606976.html" id="Form1">
<div>
<input name="__EVENTTARGET" id="__EVENTTARGET" value="" type="hidden">
<input name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" type="hidden">
<input name="__VIEWSTATE" id="__VIEWSTATE" value="" type="hidden">
</div>




        
<!--done-->
<center>
<div id="home">
<div id="header">
	<div id="blogTitle">
	<a id="lnkBlogLogo" href="http://www.cnblogs.com/i-miss-you/"><img id="blogLogo" src="logo.gif" alt=""></a>			
		
<!--done-->
<h1><a id="Header1_HeaderTitle" class="headermaintitle" href="http://www.cnblogs.com/i-miss-you/">迷失路灯</a></h1>
<h2></h2>



		
	</div><!--end: blogTitle 博客的标题和副标题 -->
	<div id="navigator">
		
<!--done-->
			<ul id="navList">
				<li><a id="MyLinks1_HomeLink" class="menu" href="http://www.cnblogs.com/">博客园</a></li>
				<li><a id="MyLinks1_SpaceLink" class="menu" href="http://space.cnblogs.com/">社区</a></li>
				<li><a id="MyLinks1_MyHomeLink" class="menu" href="http://www.cnblogs.com/i-miss-you/">首页</a></li>
				<li><a id="MyLinks1_NewPostLink" class="menu" href="http://www.cnblogs.com/i-miss-you/admin/EditPosts.aspx?opt=1">新随笔</a></li>
				<li><a id="MyLinks1_ContactLink" accesskey="9" class="menu" href="http://space.cnblogs.com/msg/send/%e8%bf%b7%e5%a4%b1%e8%b7%af%e7%81%af">联系</a></li>
				<li><a id="MyLinks1_Admin" class="menu" href="http://www.cnblogs.com/i-miss-you/admin/EditPosts.aspx">管理</a></li>
				<li><a id="MyLinks1_Syndication" class="menu" href="http://www.cnblogs.com/i-miss-you/rss">订阅</a>
				<a id="MyLinks1_XMLLink" class="aHeaderXML" href="http://www.cnblogs.com/i-miss-you/rss"><img src="rss.gif" alt="订阅" style="border-width: 0px;"></a></li>
			</ul>


		<div class="blogStats">
			
			
<!--done-->
随笔- 1&nbsp;
文章- 1&nbsp;
评论- 1&nbsp;

			
		</div><!--end: blogStats -->
	</div><!--end: navigator 博客导航栏 -->
</div><!--end: header 头部 -->

<div class="clear"></div>
	<div id="topicList">
		
					
<!--done-->
<div id="topics">
	<div class="post">
		<h1 class="postTitle">
			<a id="ctl04_TitleUrl" class="postTitle2" href="http://www.cnblogs.com/i-miss-you/articles/1606976.html">Spring.net快速上手指南</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			&nbsp; 
<p><span style="font-family: 宋体;">首先简单介绍下几个术语：</span></p>
<p style="line-height: 18pt;" align="left"><strong><span style="font-size: 14pt; color: black; font-family: Arial;">1</span></strong><strong><span style="font-size: 14pt; color: black; font-family: 宋体;">．</span></strong><strong><span style="font-size: 14pt; color: black; font-family: Arial;">O/R Mapping</span></strong></p>
<p style="text-indent: 22.5pt;"><span style="color: black; font-family: 宋体; letter-spacing: 0.4pt;">对象关系映射（</span><span style="color: black; font-family: Arial; letter-spacing: 0.4pt;">Object Relational Mapping</span><span style="color: black; font-family: 宋体; letter-spacing: 0.4pt;">，简称</span><span style="color: black; font-family: Arial; letter-spacing: 0.4pt;">ORM</span><span style="color: black; font-family: 宋体; letter-spacing: 0.4pt;">）是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术。</span> <span style="color: black; font-family: 宋体; letter-spacing: 0.4pt;">简单的说，</span><span style="color: black; font-family: Arial; letter-spacing: 0.4pt;">ORM</span><span style="color: black; font-family: 宋体; letter-spacing: 0.4pt;">是通过使用描述对象和数据库之间映射的元数据，将。</span><span style="color: black; font-family: Arial; letter-spacing: 0.4pt;">Net</span><span style="color: black; font-family: 宋体; letter-spacing: 0.4pt;">程序中的对象自动持久化到关系数据库中。本质上就是将数据从一种形式转换到另外一种形式。</span> <span style="color: black; font-family: 宋体; letter-spacing: 0.4pt;">这也同时暗示者额外的执行开销；然而，如果</span><span style="color: black; font-family: Arial; letter-spacing: 0.4pt;">ORM</span><span style="color: black; font-family: 宋体; letter-spacing: 0.4pt;">作为一种中间件实现，则会有很多机会做优化，而这些在手写的持久层并不存在。</span></p>
<p style="line-height: 18pt;" align="left"><strong><span style="font-size: 14pt; color: black; font-family: Arial;">2</span></strong><strong><span style="font-size: 14pt; color: black; font-family: 宋体;">．</span></strong><strong><span style="font-size: 14pt; color: black; font-family: Arial;">NHibernate</span></strong><span style="color: rgb(51, 51, 51); font-family: 宋体;"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(51, 51, 51); font-family: 宋体;">在今日的企业环境中，把<a title="面向对象" href="http://www.hudong.com/wiki/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1" target="_top"><span style="color: rgb(2, 104, 205); text-decoration: none;"><span>面向对象</span></span></a></span>的<a title="软件" href="http://www.hudong.com/wiki/%E8%BD%AF%E4%BB%B6" target="_top"><span style="color: rgb(2, 104, 205); text-decoration: none;"><span>软件</span></span></a>和关系数据库一起使用可能是相当麻烦和浪费时间的.NHibernate不仅仅管理.NET类到数据库表的映射（包括.NET 数据类型到SQL数据类型的映射），还提供数据查询和获取数据的方法，可以大幅度减少开发时人工使用SQL和<a title="ADO.NET" href="http://www.hudong.com/wiki/ADO.NET" target="_top"><span style="color: rgb(2, 104, 205); text-decoration: none;">ADO.NET</span></a>处理数据的时间。</p>
<p style="line-height: 18.75pt;" align="left"><span style="color: rgb(51, 51, 51); font-family: 宋体;">&nbsp;&nbsp;&nbsp;NHibernate</span><span style="color: rgb(51, 51, 51); font-family: 宋体;">的目标主要是用于与数据持久化相关的编程任务，能够使开发人员从原来枯燥的SQL语句的编写中解放出来，解放出来的精力可以让开发人员投入到业务逻辑的实现上。</span></p>
<p><strong><span style="font-size: 14pt; color: black; font-family: Arial;">3.</span></strong><strong><span style="font-size: 14pt; color: black; font-family: 宋体;">例子</span></strong><strong></strong></p>
<p style="text-indent: 10.5pt;"><span style="font-family: 新宋体;">&nbsp;&nbsp;</span><span style="font-family: 新宋体;">现在我就用一个简单的例子来向大家说明，在vs2005下如何利用spring.net框架的IOC机制通过O/R mapping机制对数据库进行访问。</span></p>
<p style="margin-left: 18pt; text-indent: -18pt;"><strong><span style="font-size: 12pt; font-family: 新宋体;">1.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">&nbsp;&nbsp; </span></span></strong><strong><span style="font-size: 12pt; font-family: 新宋体;">建立一个c/s项目命名为Springs,并且引入相应的spring.net的命名空间（如图1）[在这里需要说明的我们用的是spring.net1..2的版本，nhibernate为1.2的版本]</span></strong></p>
<p style="margin-left: 18pt; text-indent: -18pt;"><strong><span style="font-size: 12pt; font-family: 新宋体;"><span style="font-size: 10.5pt; font-family: 华文宋体;" lang="EN-US"><v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><strong>&nbsp;<v:stroke joinstyle="miter"></v:stroke></strong><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype></span></span></strong></p>
<p align="center"><strong><span style="font-family: 华文宋体;"></span></strong></p>
<p><span style="font-family: 新宋体;">
<div align="center">
<div align="center"><img alt="" src="1.jpg" border="0" height="450" width="720"></div></div>由于要通过spring.,net中的O/R mapping 机制对数据库进行访问，所以我们需要运用nhibernate这个数据持久层的框架（这个框架的命名空间在步骤1中已经实现）</span></p>
<p style="margin-left: 18pt; text-indent: -18pt;"><strong><span style="font-size: 12pt; font-family: 新宋体;">2.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">&nbsp;&nbsp; </span></span></strong><strong><span style="font-size: 12pt; font-family: 新宋体;">用sqlserver2000创建一个名为Test的数据库，并且在这个数据库中建立一个名字为myuser的数据表:</span></strong></p>
<p><strong><span style="font-family: 新宋体;">&nbsp;</span></strong></p>
<table style="border: medium none ; border-collapse: collapse;" border="1" cellpadding="0" cellspacing="0">
<tbody><tr>
<td style="border: 1pt solid windowtext; padding: 0cm 5.4pt; width: 142pt;" valign="top" width="189">
<p align="center"><span style="font-family: 新宋体;">列名</span></p></td>
<td style="border-style: solid solid solid none; border-color: windowtext windowtext windowtext -moz-use-text-color; border-width: 1pt 1pt 1pt medium; padding: 0cm 5.4pt; width: 142.05pt;" colspan="2" valign="top" width="189">
<p align="center"><span style="font-family: 新宋体;">类型</span></p></td>
<td style="border-style: solid solid solid none; border-color: windowtext windowtext windowtext -moz-use-text-color; border-width: 1pt 1pt 1pt medium; padding: 0cm 5.4pt; width: 142.3pt;" valign="top" width="190">
<p align="center"><strong><span style="font-family: 新宋体;">长度</span></strong></p></td></tr>
<tr>
<td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 142pt;" valign="top" width="189">
<p align="center"><span style="font-family: 新宋体;">userid(</span><span style="font-family: 新宋体;">主键)</span></p></td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 142.05pt;" colspan="2" valign="top" width="189">
<p align="center"><span style="font-family: 新宋体;">Int</span></p></td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 142.3pt;" valign="top" width="190">
<p align="center"><span style="font-family: 新宋体;">4</span></p></td></tr>
<tr>
<td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 142pt;" valign="top" width="189">
<p align="center"><span style="font-family: 新宋体;">username</span></p></td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 142.05pt;" colspan="2" valign="top" width="189">
<p align="center"><span style="font-family: 新宋体;">varchar</span></p></td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 142.3pt;" valign="top" width="190">
<p align="center"><span style="font-family: 新宋体;">20</span></p></td></tr>
<tr>
<td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 142pt;" valign="top" width="189">
<p align="center"><span style="font-family: 新宋体;">userpassword</span></p></td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 142.05pt;" colspan="2" valign="top" width="189">
<p align="center"><span style="font-family: 新宋体;">varchar</span></p></td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 142.3pt;" valign="top" width="190">
<p align="center"><span style="font-family: 新宋体;">20</span></p></td></tr>
<tr style="height: 15.75pt;">
<td style="border-style: none solid solid; border-color: -moz-use-text-color windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; width: 142pt; height: 15.75pt;" valign="top" width="189">
<p style="margin-left: -0.4pt;" align="center"><span style="font-family: 新宋体;">Useremail</span></p></td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 5cm; height: 15.75pt;" valign="top" width="189">
<p style="margin-left: -0.4pt;" align="center"><span style="font-family: 新宋体;">Varchar</span></p></td>
<td style="border-style: none solid solid none; border-color: -moz-use-text-color windowtext windowtext -moz-use-text-color; border-width: medium 1pt 1pt medium; padding: 0cm 5.4pt; width: 142.6pt; height: 15.75pt;" colspan="2" valign="top" width="190">
<p style="margin-left: -0.4pt;" align="center"><span style="font-family: 新宋体;">20</span></p></td></tr>
<tr height="0">
<td style="border: medium none ;" width="189"></td>
<td style="border: medium none ;" width="189"></td>
<td style="border: medium none ;" width="0"></td>
<td style="border: medium none ;" width="190"></td></tr></tbody></table>
<p><span style="font-family: 新宋体;">数据表建立好了以后，我们要做的就是在项目中为此表建立一个实体映射类</span></p>
<p style="margin-left: 18pt; text-indent: -18pt;"><strong><span style="font-size: 12pt; font-family: 新宋体;">3.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">&nbsp;&nbsp; </span></span></strong><strong><span style="font-size: 12pt; font-family: 新宋体;">建立一个实体映射类MyUser.cs</span></strong></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Collections.Generic;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Text;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">namespace</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.model<span style="color: rgb(51, 153, 102);">//</span></span><span style="font-size: 12pt; color: rgb(51, 153, 102); font-family: 新宋体;">从这我们可以知道类的命名空间，以后将不再重复</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">{</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: teal;">MyUser</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">int</span> userid;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> Userid</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> userid; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { userid = <span style="color: blue;">value</span>; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: teal;">String</span> username;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: teal;">String</span> Username</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> username; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { username = <span style="color: blue;">value</span>; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: teal;">String</span> userpassword;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: teal;">String</span> Userpassword</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> userpassword; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { userpassword = <span style="color: blue;">value</span>; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: teal;">String</span> useremail;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: teal;">String</span> Useremail</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> useremail; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { useremail = <span style="color: blue;">value</span>; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;}</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">}</span></p>
<p style="margin-left: 18pt; text-indent: -18pt; text-align: left;" align="left"><strong><span style="font-size: 12pt; font-family: 新宋体;">4.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">&nbsp;&nbsp; </span></span></strong><strong><span style="font-size: 12pt; font-family: 新宋体;">建立起表与实体类之间的关系MyUser.hbm.xml</span></strong></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&lt;?</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">xml</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">version</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">1.0</span>" <span style="color: red;">encoding</span><span style="color: blue;">=</span>"<span style="color: blue;">utf-8</span>"<span style="color: blue;"> ?&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">hibernate-mapping</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">xmlns</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">urn:nhibernate-mapping-2.2</span>"<span style="color: blue;">&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">class</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">Springs.model.MyUser,Springs</span>" <span style="color: red;">table</span><span style="color: blue;">=</span>"<span style="color: blue;">myuser</span>" <span style="color: red;">lazy</span><span style="color: blue;">=</span>"<span style="color: blue;">false</span>"<span style="color: blue;">&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">id</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">Userid</span>" <span style="color: red;">column</span><span style="color: blue;">=</span>"<span style="color: blue;">userid</span>" <span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">Int32</span>"<span style="color: blue;">&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">generator</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">class</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">native</span>"<span style="color: blue;">&nbsp;/&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">id</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">property</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">Username</span>" <span style="color: red;">column</span><span style="color: blue;">=</span>"<span style="color: blue;">username</span>" <span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">String</span>" <span style="color: red;">length</span><span style="color: blue;">=</span>"<span style="color: blue;">20</span>"<span style="color: blue;"> /&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">property</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">Userpassword</span>" <span style="color: red;">column</span><span style="color: blue;">=</span>"<span style="color: blue;">userpassword</span>" <span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">String</span>" <span style="color: red;">length</span><span style="color: blue;">=</span>"<span style="color: blue;">20</span>"<span style="color: blue;"> /&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">property</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">Useremail</span>" <span style="color: red;">column</span><span style="color: blue;">=</span>"<span style="color: blue;">useremail</span>" <span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">String</span>" <span style="color: red;">length</span><span style="color: blue;">=</span>"<span style="color: blue;">20</span>"<span style="color: blue;">&gt;&lt;/</span><span style="color: maroon;">property</span><span style="color: blue;">&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">class</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">hibernate-mapping</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="color: black; font-family: 新宋体;">至此，我们已经完成了数据表和实体类之间的映射关系</span></p>
<p style="margin-left: 18pt; text-indent: -18pt; text-align: left;" align="left"><strong><span style="font-size: 12pt; color: black; font-family: 新宋体;">5.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">&nbsp;&nbsp; </span></span></strong><strong><span style="font-size: 12pt; color: black; font-family: 新宋体;">建立一个服务接口UserService.cs</span></strong></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Collections.Generic;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Text;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.model;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Data;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">namespace</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.service</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">{</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: teal;">UserService</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">void</span> insert(<span style="color: teal;">MyUser</span> user);</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;}</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">}</span></p>
<p style="margin-left: 18pt; text-indent: -18pt; text-align: left;" align="left"><strong><span style="font-size: 12pt; color: black; font-family: 新宋体;">6.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">&nbsp;&nbsp; </span></span></strong><strong><span style="font-size: 12pt; color: black; font-family: 新宋体;">实现UserService接口（UserServiceImp.cs）</span></strong></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Spring.Data.NHibernate.Support;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.service;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.model;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Configuration;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Spring.Context;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Data;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Data.Sql;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Data.SqlClient;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> NHibernate;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">namespace</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.serviceimp</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">{</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: teal;">UserServiceImp</span>:<span style="color: teal;">HibernateDaoSupport</span>,<span style="color: teal;">UserService</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> insert(<span style="color: teal;">MyUser</span> user)</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.HibernateTemplate.Save(user);</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">}</span></p>
<p align="left"><span style="color: black; font-family: 新宋体;">在这里我需要解释的是，对于HibernateDaoSupport这个类来说我们在前面的过程中并没有提到，这个类是Spring.net框架中实现好的类，我们只要实现它，并且在spring.net的xml文件中（稍候将会看到）进行相应的配置，就可以通过它来调用HibernateTemplate对数据库进行映射操作了。</span></p>
<p align="left"><strong><span style="font-size: 12pt; color: black; font-family: 新宋体;">7</span></strong><strong><span style="font-size: 12pt; color: black; font-family: 新宋体;">．编写一个可以利用UserService服务的类UserManager.cs</span></strong></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Collections.Generic;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Text;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.serviceimp;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.service;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.model;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">namespace</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.usermanager</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">{</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: teal;">UserManager</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: teal;">UserService</span> us;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: teal;">UserService</span> Us</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> us; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { us = <span style="color: blue;">value</span>; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> insertUser()</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">MyUser</span> user = <span style="color: blue;">new</span> <span style="color: teal;">MyUser</span>();</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; user.Username = <span style="color: maroon;">"zhenxin"</span>;</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; user.Userpassword = <span style="color: maroon;">"hahaha"</span>;</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; user.Useremail = <span style="color: maroon;"><span>"<a href="mailto:1s2k3y4@sina.com" class="smarterwiki-linkify">1s2k3y4@sina.com</a>"</span></span>;</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; us.insert(user);</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">}</span></p>
<p align="left"><span style="color: black; font-family: 新宋体;">在这里我们利用属性形式(如果不明白什么是属性形式注入，请参看《业务规则层案例4.0.1》)将服务UserService注入在了UserManager中（稍候将看到spring.net的xml文件），在insertUser()方法中调用UserService的服务，直接对数据实体类MyUser进行操作。</span></p>
<p align="left"><span style="color: black; font-family: 新宋体;">&nbsp;&nbsp; </span><span style="color: black; font-family: 新宋体;">为了我们可以很好的管理所有的服务，我们需要创建一个服务器容器ServiceContainer，在这里，我们可以随时调用我们需要的服务，创建这个类的好处是，可以对服务进行管理，方便于调用。</span></p>
<p align="left"><span style="font-size: 12pt; color: black; font-family: 新宋体;">&nbsp;</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Collections.Generic;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Text;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Spring.Context;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Configuration;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Spring.Context.Support;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.service;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">namespace</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.tools</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">{</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: teal;">ServiceContainer</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: teal;">UserService</span> userServices;</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: teal;">IApplicationContext</span> context;</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: teal;">ServiceContainer</span> serviceContainer = <span style="color: blue;">null</span>;</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> ServiceContainer()</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: teal;">ServiceContainer</span> Servicecontainer</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (serviceContainer == <span style="color: blue;">null</span>)</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; serviceContainer = <span style="color: blue;">new</span> <span style="color: teal;">ServiceContainer</span>();</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context = <span style="color: teal;">ContextRegistry</span>.GetContext();</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">return</span> serviceContainer;</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: teal;">UserService</span> UserServices</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; userServices = context.GetObject(<span style="color: maroon;">"UserService"</span>) <span style="color: blue;">as</span> <span style="color: teal;">UserService</span>;</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> userServices;</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green;">//</span></span><span style="font-size: 12pt; color: green; font-family: 新宋体;">通过IOC注入在这里得到UserService实例对象</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">}</span></p>
<p align="left"><span style="font-family: 新宋体;">此类为设计成单例的原因是，只允许一个ServiceContainer的产生，所有的服务都通过这一个ServiceContainer进行调用。</span></p>
<p align="left">&nbsp;</p>
<p align="left">&nbsp;</p>
<p align="left">&nbsp;</p>
<p align="left">&nbsp;</p>
<p align="left">&nbsp;</p>
<p align="left">&nbsp;</p>
<p align="left">&nbsp;</p>
<p style="margin-left: 18pt; text-indent: -18pt; text-align: left;" align="left"><span style="font-size: 12pt; font-family: 新宋体;">7.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">&nbsp;&nbsp;&nbsp; </span></span><strong><span style="font-size: 12pt; font-family: 新宋体;">配置app.config文件</span></strong></p>
<p align="left"><span style="font-family: 新宋体;">为了将spring.net框架加入到我们的项目中我们需要在项目启动的时候就要对spring.net 进行加载，我们将spring.net框架的xml文件单独放在一个文件user_spring.xml中，在app.config中我们只要引用就可以了。</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&lt;?</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">xml</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">version</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">1.0</span>"<span style="color: blue;">?&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">configuration</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">configSections</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">sectionGroup</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">spring</span>"<span style="color: blue;">&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">section</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">context</span>" <span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">Spring.Context.Support.ContextHandler, Spring.Core</span>"<span style="color: blue;"> /&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">section</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">objects</span>" <span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">Spring.Context.Support.DefaultSectionHandler, Spring.Core</span>"<span style="color: blue;"> /&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">sectionGroup</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">configSections</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">spring</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">context</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">resource</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">uri</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;"><span><a href="http://www.cnblogs.com/user_spring.xml" class="smarterwiki-linkify">http://www.cnblogs.com/user_spring.xml</a></span></span>"<span style="color: blue;">&gt;&lt;/</span><span style="color: maroon;">resource</span><span style="color: blue;">&gt;</span><span style="color: rgb(51, 153, 102);">//</span></span><span style="font-size: 12pt; color: rgb(51, 153, 102); font-family: 新宋体;">引用文件</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">resource</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">uri</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">config://spring/objects</span>"<span style="color: blue;">/&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">context</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">objects</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">xmlns</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;"><span><a href="http://www.springframework.net/" class="smarterwiki-linkify">http://www.springframework.net</a></span></span>"<span style="color: blue;">/&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">spring</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">appSettings</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">/&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">connectionStrings</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">/&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">configuration</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left">&nbsp;</p>
<p align="left">&nbsp;</p>
<p align="left">&nbsp;</p>
<p align="left">&nbsp;</p>
<p align="left"><strong><span style="font-size: 12pt; color: black; font-family: 新宋体;">8</span></strong><strong><span style="font-size: 12pt; color: black; font-family: 新宋体;">．配置user_spring.xml</span></strong></p>
<p align="left"><span style="color: black; font-family: 新宋体;">这是我们的核心配置文件。</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&lt;?</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">xml</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">version</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">1.0</span>" <span style="color: red;">encoding</span><span style="color: blue;">=</span>"<span style="color: blue;">utf-8</span>"<span style="color: blue;"> ?&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">objects</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">xmlns</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;"><span><a href="http://www.springframework.net/" class="smarterwiki-linkify">http://www.springframework.net</a></span></span>"</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size: 12pt; color: red; font-family: 新宋体;">xmlns:xsi</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;"><span><a href="http://www.w3.org/2001/XMLSchema-instance" class="smarterwiki-linkify">http://www.w3.org/2001/XMLSchema-instance</a></span></span>"</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size: 12pt; color: red; font-family: 新宋体;">xsi:schemaLocation</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;"><span><a href="http://www.springframework.net/" class="smarterwiki-linkify">http://www.springframework.net</a> <a href="http://www.springframework.net/xsd/spring-objects.xsd" class="smarterwiki-linkify">http://www.springframework.net/xsd/spring-objects.xsd</a></span></span>"<span style="color: blue;">&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">object</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">id</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">DbProvider</span>" <span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">Spring.Data.Common.DbProviderFactoryObject,Spring.Data</span>"<span style="color: blue;">&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">property</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">Provider</span>" <span style="color: red;">value</span><span style="color: blue;">=</span>"<span style="color: blue;">SqlServer-2.0</span>"<span style="color: blue;">/&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">property</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">ConnectionString</span>" <span style="color: red;">value</span><span style="color: blue;">=</span>"<span style="color: blue;">Data Source=.;Database=Test;User ID=sa;Password=;Trusted_Connection=False</span>"<span style="color: blue;"> /&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">object</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">object</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">id</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">SessionFactory</span>" <span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12</span>"<span style="color: blue;">&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">property</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">DbProvider</span>" <span style="color: red;">ref</span><span style="color: blue;">=</span>"<span style="color: blue;">DbProvider</span>"<span style="color: blue;"> /&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">property</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">MappingAssemblies</span>"<span style="color: blue;">&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">list</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">value</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span><span style="font-size: 12pt; font-family: 新宋体;">Springs<span style="color: blue;">&lt;/</span><span style="color: maroon;">value</span><span style="color: blue;">&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">list</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">property</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">property</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">HibernateProperties</span>"<span style="color: blue;">&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">dictionary</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">entry</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">key</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">hibernate.connection.provider</span>" <span style="color: red;">value</span><span style="color: blue;">=</span>"<span style="color: blue;">NHibernate.Connection.DriverConnectionProvider</span>"<span style="color: blue;"> /&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">entry</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">key</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">hibernate.dialect</span>" <span style="color: red;">value</span><span style="color: blue;">=</span>"<span style="color: blue;">NHibernate.Dialect.MsSql2000Dialect</span>"<span style="color: blue;"> /&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">entry</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">key</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">hibernate.connection.driver_class</span>" <span style="color: red;">value</span><span style="color: blue;">=</span>"<span style="color: blue;">NHibernate.Driver.SqlClientDriver</span>"<span style="color: blue;"> /&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">entry</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">key</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">show_sql</span>" <span style="color: red;">value</span><span style="color: blue;">=</span>"<span style="color: blue;">true</span>"<span style="color: blue;"> /&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">dictionary</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">property</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">object</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">object</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">id</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">UserService</span>" <span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">Springs.serviceimp.UserServiceImp</span>"<span style="color: blue;">&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">property</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">SessionFactory</span>" <span style="color: red;">ref</span><span style="color: blue;">=</span>"<span style="color: blue;">SessionFactory</span>"<span style="color: blue;"> /&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">object</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">object</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">id</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">UserManager</span>" <span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">Springs.usermanager.UserManager</span>"<span style="color: blue;">&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &lt;</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">property</span> <span style="font-size: 12pt; color: red; font-family: 新宋体;">name</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">=</span><span style="font-size: 12pt; font-family: 新宋体;">"<span style="color: blue;">Us</span>" <span style="color: red;">ref</span><span style="color: blue;">=</span>"<span style="color: blue;">UserService</span>"<span style="color: blue;"> /&gt;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&nbsp;&lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">object</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&lt;/</span><span style="font-size: 12pt; color: maroon; font-family: 新宋体;">objects</span><span style="font-size: 12pt; color: blue; font-family: 新宋体;">&gt;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="color: black; font-family: 新宋体;">在这个文件中有SessoionFactory和DbProvider这两个节点的配置应该多多注意下。同时还注入了UserService服务，还有UserManager这个将服务以属性方式注入到自身里的类。</span></p>
<p align="left"><strong><span style="font-size: 12pt; color: black; font-family: 新宋体;">9</span></strong><strong><span style="font-size: 12pt; color: black; font-family: 新宋体;">．测试</span></strong></p>
<p align="left"><span style="color: black; font-family: 新宋体;">(1) </span><span style="color: black; font-family: 新宋体;">第一种：从ServiceContainer中得到服务，对数据库进行插入操作</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Spring.Context;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> System.Configuration;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.usermanager;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.service;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.model;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Spring.Core;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Spring.Aop;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Spring.Context.Support;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">using</span><span style="font-size: 12pt; font-family: 新宋体;"> Springs.tools;</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">private</span><span style="font-size: 12pt; font-family: 新宋体;"> <span style="color: blue;">void</span> Form1_Load(<span style="color: blue;">object</span> sender, <span style="color: teal;">EventArgs</span> e)</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; color: teal; font-family: 新宋体;">UserService</span><span style="font-size: 12pt; font-family: 新宋体;"> sc= <span style="color: teal;">ServiceContainer</span>.Servicecontainer.UserServices;</span></p>
<p align="left"><span style="font-size: 12pt; color: teal; font-family: 新宋体;">MyUser</span><span style="font-size: 12pt; font-family: 新宋体;"> user = <span style="color: blue;">new</span> <span style="color: teal;">MyUser</span>();</span></p>
<p><span style="font-size: 12pt; font-family: 新宋体;">user.Username = <span style="color: maroon;">"XXXXXXX"</span>;</span></p>
<p><span style="font-size: 12pt; font-family: 新宋体;">user.Userpassword = <span style="color: maroon;">"hahaha"</span>;</span></p>
<p><span style="font-size: 12pt; font-family: 新宋体;">user.Useremail = <span style="color: maroon;"><span>"<a href="mailto:1s2k3y4@sina.com" class="smarterwiki-linkify">1s2k3y4@sina.com</a>"</span></span>;</span></p>
<p><span style="font-size: 12pt; font-family: 新宋体;">sc.insert(user);</span></p>
<p style="text-indent: 24pt;" align="left"><span style="font-size: 12pt; font-family: 新宋体;">}</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">（</span><span style="font-family: 新宋体;">2</span><span style="font-family: 新宋体;">）第二种：通过ServiceContainer调用服务，对数据库进行插入操作</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">private</span><span style="font-size: 12pt; font-family: 新宋体;"> <span style="color: blue;">void</span> Form1_Load(<span style="color: blue;">object</span> sender, <span style="color: teal;">EventArgs</span> e)</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; color: teal; font-family: 新宋体;">IApplicationContext</span><span style="font-size: 12pt; font-family: 新宋体;"> context= <span style="color: teal;">ContextRegistry</span>.GetContext();</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp; <span style="color: teal;">UserManager</span> um = (<span style="color: teal;">UserManager</span>)context.GetObject(<span style="color: maroon;">"UserManager"</span>);</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp; um.insertUser();</span></p>
<p style="text-indent: 24pt;" align="left"><span style="font-size: 12pt; font-family: 新宋体;">}</span></p>
<p align="left">&nbsp;</p>
<p align="left"><strong><span style="font-size: 12pt; font-family: 新宋体;">10.</span></strong><strong><span style="font-size: 12pt; font-family: 新宋体;">功能扩展</span></strong></p>
<p align="left"><span style="font-family: 新宋体;">在上述的例子中展示了spring.net结合nhibernate集合框架对数据库进行插入操作的实现，现在要将此功能扩展，使读者更加深刻的体会到nhibernate是如何对数据库进行操作的。下面是扩展后的小例子，我们可以在这里进行增、删、该、查的功能</span></p>
<p style="margin-left: 36pt; text-indent: -36pt;"><strong><span style="font-family: 新宋体;">（1）<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;">&nbsp;&nbsp;&nbsp;&nbsp; </span></span></strong><strong><span style="font-family: 新宋体;">实现全部查询功能</span></strong></p><strong><span style="font-family: 新宋体;">
<div align="center"><img alt="" src="2.jpg" border="0" height="450" width="720"></div></span></strong>
<p>&nbsp;</p>
<p align="left"><span style="font-family: 新宋体;">&nbsp;</span><span style="font-family: 新宋体;">当你单击显示数据的时候，所有从数据库中得到的数据将会全部显示在上述的dataGridView中，显示如下：</span></p>
<p align="left"><span style="font-family: 新宋体;">
<div align="center">
<div align="center"><img alt="" src="3.jpg" border="0" height="450" width="720"></div></div></span></p>
<p align="left"><span style="font-family: 新宋体;">&nbsp;功能的实现还是通过UserServiceImp来实现UserService中定义的SelectAll()方法代码片断如下：</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">public</span><span style="font-size: 12pt; font-family: 新宋体;"> <span style="color: teal;">IList</span> SelectAll() {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">&nbsp;//</span></span><span style="font-size: 12pt; color: green; font-family: 新宋体;">第一种实现方式</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">// IQuery q = this.Session.CreateQuery("From MyUser");</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//IList list=q.List();</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//return list;</span></span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; color: green; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-size: 12pt; color: green; font-family: 新宋体;">第二种实现方式</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">IList</span> list =<span style="color: blue;">this</span>.HibernateTemplate.LoadAll(<span style="color: blue;">typeof</span>(<span style="color: teal;">MyUser</span>));</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> list;</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp; </span><span style="font-family: 新宋体;">&nbsp;</span><span style="font-family: 新宋体;">在这里我要说明的是:这里有两种方式来实现查询，第一种是通过功能强大的hql查询语言，第二种是通过HibernateTemplate.LoadAll()方法来进行。</span></p>
<p align="left"><span style="font-family: 新宋体;">&nbsp;&nbsp; </span><span style="font-family: 新宋体;">第一种方式的优点是：查询功能灵活，可以根据用户来自定义查询语句，可以实现多条查询或者模糊查询。但是缺点是，增加了程序员的工作量，因为要自己写查询语句。</span></p>
<p align="left"><span style="font-family: 新宋体;">&nbsp;</span><span style="font-family: 新宋体;">第二种方式的优点是：可以简便的实现查询功能，代码的规则统一，但是缺点是功能不够强大，没有办法通过用户自定义来编写需要的查询语句。</span></p>
<p align="left"><strong><span style="font-family: 新宋体;">(2)</span></strong><strong><span style="font-family: 新宋体;">实现删除功能</span></strong></p>
<p align="left"><span style="font-family: 新宋体;">&nbsp;&nbsp; </span><span style="font-family: 新宋体;">同样的删除功能的实现也是通过，在UserServiceImp中实现UsersService接口中的deleteById()方法（这只是一个方法名称没有具体意义）</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">public</span><span style="font-size: 12pt; font-family: 新宋体;"> <span style="color: blue;">void</span> deleteById(<span style="color: teal;">MyUser</span> user)</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.HibernateTemplate.Delete(user);</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;}</span></p>
<p align="left"><span style="font-family: 新宋体;">&nbsp;</span><span style="font-family: 新宋体;">在这里需要指出（以此程序为例）的我们在传递参数MyUser的时候，不需要将MyUser的所有属性赋值，我们只需要将这个实体类中对应映射数据表中的主键的属性赋值就可以完成效果，这样也大大地简化了程序的代码量。</span></p>
<p align="left"><strong><span style="font-family: 新宋体;">(3) </span></strong><strong><span style="font-family: 新宋体;">实现按条件查询</span></strong></p>
<p align="left"><strong><span style="font-family: 新宋体;"></span></strong><span style="font-family: 新宋体;">当按下“按ID查询”出现一条记录</span></p>
<p align="left"><span style="font-family: 新宋体;">
<div align="center"><img alt="" src="4.jpg" border="0" height="450" width="720"></div></span></p>
<p align="left"></p>
<p align="left"><span style="font-family: 新宋体;">在UserServiceImp中的代码片断是：</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">public</span><span style="font-size: 12pt; font-family: 新宋体;"> <span style="color: teal;">MyUser</span> selectOneById(<span style="color: blue;">int</span> userid)</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp; <span style="color: green;">//</span></span><span style="font-size: 12pt; color: green; font-family: 新宋体;">第一种方式</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//IQuery q = this.Session.CreateQuery("From MyUser where Userid=?");</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//q.SetInt32(0, userid);</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//IList list = q.List();</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//MyUser user = (MyUser)list[0];</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: green;">//return user;</span></span></p>
<p align="left"><span style="font-size: 12pt; color: green; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="font-size: 12pt; color: green; font-family: 新宋体;">第二种方式</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> HibernateTemplate.Load(<span style="color: blue;">typeof</span>(<span style="color: teal;">MyUser</span>), userid) <span style="color: blue;">as</span> <span style="color: teal;">MyUser</span>;</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;}</span></p>
<p style="text-indent: 10.5pt;" align="left"><span style="font-family: 新宋体;">在第一种实现方式中，展现了使用hql语句的传参实现方式,所以可以利用hql这种实现方式，进行多条件查找，和模糊查找，弹性非常好。</span></p>
<p style="text-indent: 10.5pt;" align="left">&nbsp;</p>
<p style="text-indent: 10.55pt;" align="left"><strong><span style="font-family: 新宋体;">(4)</span></strong><strong><span style="font-family: 新宋体;">关于更新</span></strong></p>
<p style="text-indent: 10.5pt;" align="left"><span style="font-family: 新宋体;">关于更新其实和插入的方式差不多，在这里就不赘述了</span></p>
<p style="text-indent: 12.05pt;" align="left"><strong>&nbsp;</strong></p>
<p style="text-indent: 12.05pt;" align="left"><strong><span style="font-size: 12pt; font-family: 新宋体;">11</span></strong><strong><span style="font-size: 12pt; font-family: 新宋体;">．关于NHibernate对象关联映射</span></strong></p>
<p style="text-indent: 10.5pt;" align="left"><span style="font-family: 新宋体;">&nbsp;&nbsp; </span><span style="font-family: 新宋体;">现在我们以另外一个小例子来讲述以下什么是对象关联映射。</span></p>
<p style="text-indent: 10.5pt;" align="left"><span style="font-family: 新宋体;">在数据库的表中避免不了要在表和表之间产生关联，一对多的关系最为常见，比如说一张存储父亲信息的表中，会有很多的父亲信息，当然，对于这个表可能会对应一张存储儿子的表，但是很显然，对于一个父亲而言，可能会对应很多的儿子这样就形成了一对多的关系，但是我们要在实体映射中如何体现这样的关系呢？</span></p>
<p style="text-indent: 10.5pt;" align="left"><span style="font-family: 新宋体;">（1）首先我们需要在数据库中建立起这样的对应关系</span></p>
<p style="text-indent: 10.5pt;" align="left"><span style="font-family: 新宋体;">
<div align="center"><img alt="" src="5.jpg" border="0" height="450" width="720"></div></span></p>
<p style="text-indent: 10.5pt;" align="left"></p>
<p align="left"><span style="font-size: 12pt; color: black; font-family: 新宋体;">在Parent.cs中：</span></p>
<p align="left"><span style="font-size: 12pt; color: blue; font-family: 新宋体;">public</span><span style="font-size: 12pt; font-family: 新宋体;"> <span style="color: blue;">class</span> <span style="color: teal;">Parent</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">int</span> parentId;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> ParentId</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> parentId; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { parentId = <span style="color: blue;">value</span>; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;}</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">string</span> name;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> name; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { name = <span style="color: blue;">value</span>; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: teal;">IList</span> children;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: teal;">IList</span> Children</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> children; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { children = <span style="color: blue;">value</span>; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-indent: 24pt;" align="left"><span style="font-size: 12pt; font-family: 新宋体;">}</span></p>
<p style="text-indent: 24pt;" align="left">&nbsp;</p>
<p style="text-indent: 24pt;" align="left">&nbsp;</p>
<p style="text-indent: 24pt;" align="left"><span style="font-size: 12pt; font-family: 新宋体;">在Child.cs中的代码：</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: teal;">Child</span></span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">int</span> childId;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">int</span> ChildId</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> childId; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { childId = <span style="color: blue;">value</span>; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<span style="color: blue;">private</span> <span style="color: blue;">string</span> name;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> name; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { name = <span style="color: blue;">value</span>; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">int</span> parentId;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: teal;">Parent</span> parent;</span></p>
<p align="left">&nbsp;</p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: teal;">Parent</span> Parent</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> parent; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { parent = <span style="color: blue;">value</span>; }</span></p>
<p align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-indent: 12pt;" align="left"><span style="font-size: 12pt; font-family: 新宋体;">&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-indent: 12.05pt;" align="left"><strong><span style="font-size: 12pt; font-family: 新宋体;">请仔细观察中间的属性字段。</span></strong></p>
<p style="text-indent: 12pt;" align="left"><span style="font-size: 12pt; font-family: 新宋体;">Parent.hbm.xml</span></p>
<p style="text-indent: 10.5pt;" align="left"><span style="color: blue; font-family: Verdana;">&lt;?</span><span style="color: fuchsia; font-family: Verdana;">xml&nbsp;version="1.0"&nbsp;encoding="utf-8"&nbsp;</span><span style="color: blue; font-family: Verdana;">?&gt;</span><span style="color: black; font-family: Verdana;"><br></span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">hibernate-mapping&nbsp;</span><span style="color: red; font-family: Verdana;">xmlns</span><span style="color: blue; font-family: Verdana;">="urn:nhibernate-mapping-2.0"&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">class&nbsp;</span><span style="color: red; font-family: Verdana;">name</span><span style="color: blue; font-family: Verdana;">="</span><span style="color: blue; font-family: 宋体;">路径</span><span style="color: blue; font-family: Verdana;">.Parent,</span><span style="color: blue; font-family: 宋体;">项目名称</span><span style="color: blue; font-family: Verdana;">"</span><span style="color: red; font-family: Verdana;">&nbsp;table</span><span style="color: blue; font-family: Verdana;">="T_Parent"&gt;</span><span style="color: black; font-family: Verdana;"><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">id&nbsp;</span><span style="color: red; font-family: Verdana;">name</span><span style="color: blue; font-family: Verdana;">="ParentId"</span><span style="color: red; font-family: Verdana;">&nbsp;column</span><span style="color: blue; font-family: Verdana;">="ParentId"</span><span style="color: red; font-family: Verdana;">&nbsp;type</span><span style="color: blue; font-family: Verdana;">="Int32"</span><span style="color: red; font-family: Verdana;">&nbsp;unsaved-value</span><span style="color: blue; font-family: Verdana;">="0"&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">generator&nbsp;</span><span style="color: red; font-family: Verdana;">class</span><span style="color: blue; font-family: Verdana;">="native"/&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;/</span><span style="color: maroon; font-family: Verdana;">id</span><span style="color: blue; font-family: Verdana;">&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">property&nbsp;</span><span style="color: red; font-family: Verdana;">column</span><span style="color: blue; font-family: Verdana;">="Name"</span><span style="color: red; font-family: Verdana;">&nbsp;type</span><span style="color: blue; font-family: Verdana;">="String"</span><span style="color: red; font-family: Verdana;">&nbsp;name</span><span style="color: blue; font-family: Verdana;">="Name"</span><span style="color: red; font-family: Verdana;">&nbsp;length</span><span style="color: blue; font-family: Verdana;">="64"</span>&nbsp;<span style="color: blue; font-family: Verdana;">/&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">set&nbsp;</span><span style="color: red; font-family: Verdana;">name</span><span style="color: blue; font-family: Verdana;">="Children"</span><span style="color: red; font-family: Verdana;">&nbsp;cascade</span><span style="color: blue; font-family: Verdana;">="all"</span><span style="color: red; font-family: Verdana;">&nbsp;inverse</span><span style="color: blue; font-family: Verdana;">="true"</span><span style="color: red; font-family: Verdana;">&nbsp;lazy</span><span style="color: blue; font-family: Verdana;">="true"&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">key&nbsp;</span><span style="color: red; font-family: Verdana;">column</span><span style="color: blue; font-family: Verdana;">="ParentId"/&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">one-to-many&nbsp;</span><span style="color: red; font-family: Verdana;">class</span><span style="color: blue; font-family: Verdana;">="</span><span style="color: blue; font-family: 宋体;">路径</span><span style="color: blue; font-family: Verdana;">/Child,</span><span style="color: blue; font-family: 宋体;">项目名称</span><span style="color: blue; font-family: Verdana;">"/&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;/</span><span style="color: maroon; font-family: Verdana;">set</span><span style="color: blue; font-family: Verdana;">&gt;</span><span style="color: black; font-family: Verdana;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;/</span><span style="color: maroon; font-family: Verdana;">class</span><span style="color: blue; font-family: Verdana;">&gt;</span><span style="color: black; font-family: Verdana;"><br></span><span style="color: blue; font-family: Verdana;">&lt;/</span><span style="color: maroon; font-family: Verdana;">hibernate-mapping</span><span style="color: blue; font-family: Verdana;">&gt;</span></p>
<p style="text-indent: 10.5pt;" align="left">&nbsp;</p>
<p style="text-indent: 10.5pt;" align="left"><span style="color: black; font-family: Verdana;">Child.hbm.xml</span></p>
<p style="text-indent: 10.5pt;" align="left"><span style="color: blue; font-family: Verdana;">&lt;?</span><span style="color: fuchsia; font-family: Verdana;">xml&nbsp;version="1.0"&nbsp;encoding="utf-8"&nbsp;</span><span style="color: blue; font-family: Verdana;">?&gt;</span><span style="color: black; font-family: Verdana;"><br></span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">hibernate-mapping&nbsp;</span><span style="color: red; font-family: Verdana;">xmlns</span><span style="color: blue; font-family: Verdana;">="urn:nhibernate-mapping-2.0"&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">class&nbsp;</span><span style="color: red; font-family: Verdana;">name</span><span style="color: blue; font-family: Verdana;">="</span><span style="color: blue; font-family: 宋体;">路径</span><span style="color: blue; font-family: Verdana;">.Child,</span><span style="color: blue; font-family: 宋体;">项目名称</span><span style="color: blue; font-family: Verdana;">"</span><span style="color: red; font-family: Verdana;">&nbsp;table</span><span style="color: blue; font-family: Verdana;">="T_Child"&gt;</span><span style="color: black; font-family: Verdana;"><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">id&nbsp;</span><span style="color: red; font-family: Verdana;">name</span><span style="color: blue; font-family: Verdana;">="ChildId"</span><span style="color: red; font-family: Verdana;">&nbsp;column</span><span style="color: blue; font-family: Verdana;">="ChildId"</span><span style="color: red; font-family: Verdana;">&nbsp;type</span><span style="color: blue; font-family: Verdana;">="Int32"</span><span style="color: red; font-family: Verdana;">&nbsp;unsaved-value</span><span style="color: blue; font-family: Verdana;">="0"&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">generator&nbsp;</span><span style="color: red; font-family: Verdana;">class</span><span style="color: blue; font-family: Verdana;">="native"/&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;/</span><span style="color: maroon; font-family: Verdana;">id</span><span style="color: blue; font-family: Verdana;">&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">property&nbsp;</span><span style="color: red; font-family: Verdana;">column</span><span style="color: blue; font-family: Verdana;">="Name"</span><span style="color: red; font-family: Verdana;">&nbsp;type</span><span style="color: blue; font-family: Verdana;">="String"</span><span style="color: red; font-family: Verdana;">&nbsp;name</span><span style="color: blue; font-family: Verdana;">="Name"</span><span style="color: red; font-family: Verdana;">&nbsp;length</span><span style="color: blue; font-family: Verdana;">="64"</span>&nbsp;<span style="color: blue; font-family: Verdana;">/&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;</span><span style="color: maroon; font-family: Verdana;">many-to-one&nbsp;</span><span style="color: red; font-family: Verdana;">name</span><span style="color: blue; font-family: Verdana;">="Parent"</span><span style="color: red; font-family: Verdana;">&nbsp;column</span><span style="color: blue; font-family: Verdana;">="ParentId"</span><span style="color: red; font-family: Verdana;">&nbsp;class</span><span style="color: blue; font-family: Verdana;">="</span><span style="color: blue; font-family: 宋体;">路径</span><span style="color: blue; font-family: Verdana;">.Parent,</span><span style="color: blue; font-family: 宋体;">项目名称</span>&nbsp;<span style="color: blue; font-family: Verdana;">/&gt;</span><span style="color: black; font-family: Verdana;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: blue; font-family: Verdana;">&lt;/</span><span style="color: maroon; font-family: Verdana;">class</span><span style="color: blue; font-family: Verdana;">&gt;</span><span style="color: black; font-family: Verdana;"><br></span><span style="color: blue; font-family: Verdana;">&lt;/</span><span style="color: maroon; font-family: Verdana;">hibernate-mapping</span><span style="color: blue; font-family: Verdana;">&gt;</span></p>
<p style="text-indent: 10.5pt;" align="left">&nbsp;</p>
<p style="text-indent: 10.5pt;" align="left"><span style="font-family: 新宋体;">在NHibernate配置文件中使用&lt;set&gt;, &lt;list&gt;, &lt;map&gt;, &lt;bag&gt;, &lt;array&gt; 和 &lt;primitive-array&gt;等元素来定义集合。&lt;bag&gt;是典型的一个，代码中我们用IList和它对应。我们以后会详细讲集合这个话题。</span></p>
<p style="text-indent: 10.5pt;" align="left"><span style="font-family: 新宋体;">lazy</span><span style="font-family: 新宋体;">表示允许延迟加载。表示在需要使用时才加载需要的数据。例如使用lazy时我们Load一个Parent他的Children为空，只有我们访问它的某一个Child时数据才会被加载；而不设置lazy我们Load一个Parent时其Children将同时加载。注意：使用lazy加载必须保证对应ISession的打开，否则懒加载会失败。</span></p>
<p style="text-indent: 10.5pt;" align="left"><span style="font-family: 新宋体;">one-to-many</span><span style="font-family: 新宋体;">可以设置单向和双向映射，设置单向时Child一段不设置many-to-one，而设置了ParentId的属性。</span></p>
<p style="text-indent: 10.5pt;" align="left"><span style="font-family: 新宋体;">双向映射需要设置inverse而单向不需要。</span></p>
<p style="text-indent: 10.5pt;" align="left"><span style="font-family: 新宋体;">单项映射在cascade时会对把T_Child的ParentId使用Update为Null的操作。</span></p>
<p style="text-indent: 10.5pt;" align="left"><span style="font-family: 新宋体;">建议尽量使用双向映射。</span></p>
<p style="text-indent: 10.5pt;" align="left">&nbsp;</p>
<p style="line-height: 18.75pt;" align="left"><strong>&nbsp;</strong></p>
<div id="digg_block">
<div id="div_digg">										
	<div class="diggit" onclick="DiggIt(1606976,63107,1)"> 
		<span class="diggnum" id="digg_count_1606976">0</span>
	</div>
	<div class="buryit" onclick="DiggIt(1606976,63107,2)"> 
		<span class="burynum" id="bury_count_1606976">0</span>
	</div>
	<div class="clear"></div>
	<span style="display: none;" id="span_isdigged_1606976">0</span>	
	<div class="diggword" id="digg_word_1606976">(请您对文章做出评价)</div>	
</div>
</div>
<div class="clear"></div>
<div id="post_next_prev">

<a href="http://www.cnblogs.com/i-miss-you/archive/2009/11/20/1607001.html">» </a> 下一篇：<a href="http://www.cnblogs.com/i-miss-you/archive/2009/11/20/1607001.html" title="发布于2009-11-20 16:11">业务规则层设计说明</a><br>
</div>








		</div>
		<div class="postDesc">posted @ 2009-11-20 15:46 <a href="http://www.cnblogs.com/i-miss-you/">迷失路灯</a> 阅读(93) <a href="#commentform">评论(1)</a> &nbsp;<a href="http://www.cnblogs.com/i-miss-you/admin/EditArticles.aspx?postid=1606976">编辑</a> <a href="http://www.cnblogs.com/i-miss-you/AddToFavorite.aspx?id=1606976">收藏</a> <a href="#" onclick="PutInWz();return false;">网摘</a>  </div>
	</div>
	<img src="1606976.html" height="1" width="1">
	
<!--
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:Description
rdf:about="http://www.cnblogs.com/i-miss-you/articles/1606976.html"
dc:identifier="http://www.cnblogs.com/i-miss-you/articles/1606976.html"
dc:title=""
trackback:ping="http://www.cnblogs.com/i-miss-you/services/trackbacks/1606976.aspx" />
</rdf:RDF>
-->

</div><!--end: topics 文章、评论容器-->
<a name="pagedcomment"></a>
<!--done-->
<br>
<div class="feedback_area_title">发表评论</div>
<div class="feedbackNoItems"><span id="span_comment_maxid" style="display: none;">1724521</span></div>
	

		<div class="feedbackItem">
			<div class="feedbackListSubtitle">
				<div class="feedbackManage">
					&nbsp;&nbsp;<a onclick='ReplyComment("陛下",1724521,"Ct7IDZPu0Ymz78E5QmjRzzByMj8Gyk2vMIump6AzyRiPjzmUsNzVcQ==")' href="http://www.cnblogs.com/i-miss-you/articles/1606976.html#commentform">回复</a>&nbsp;&nbsp;<a onclick='QuoteComment(1724521,"Ct7IDZPu0Ymz78E5QmjRzzByMj8Gyk2vMIump6AzyRiPjzmUsNzVcQ==")' href="http://www.cnblogs.com/i-miss-you/articles/1606976.html#commentform">引用</a>&nbsp;&nbsp;<a title="查看该作者发表过的评论" href="http://www.cnblogs.com/CommentsByAuthor.aspx?author=%e9%99%9b%e4%b8%8b" target="_top">查看</a>&nbsp;&nbsp;<a id="Comments1_CommentList_ctl00_DeleteLink" href="javascript:__doPostBack('Comments1$CommentList$ctl00$DeleteLink','')"></a>&nbsp;&nbsp;<a id="Comments1_CommentList_ctl00_EditLink" causesvalidation="False"></a>
				</div>
				<a href="#1724521">#1楼</a><a name="1724521" id="comment_anchor_1724521"></a><a name="Post"></a>2009-12-15 13:53 | <a id="Comments1_CommentList_ctl00_NameLink" href="http://www.cnblogs.com/howard-queen/" target="_top">陛下</a>&nbsp;<a href="http://space.cnblogs.com/msg/send/%e9%99%9b%e4%b8%8b" title="给此人发送站内短消息" class="sendMsg2This">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
			</div>
			<div class="feedbackCon">
				<span id="comment_body_1724521" class="blog_comment_body">这文章可以发首页嘛，给我扫扫盲:)</span><br>
			</div>
		</div>
	

<div id="ctl05_comment_form" class="commentform">




<div id="ctl05_divLoginPannel">
<div class="login_tips">注册用户登录后才能发表评论，请 <a href="http://passport.cnblogs.com/login.aspx?ReturnUrl=http%3a%2f%2fwww.cnblogs.com%2fi-miss-you%2farticles%2f1606976.html%3flogin%3d1%23commentform" class="underline">登录</a> 或 <a href="http://passport.cnblogs.com/register.aspx?ReturnUrl=http%3a%2f%2fwww.cnblogs.com%2fi-miss-you%2farticles%2f1606976.html%23Bottom2" class="underline">注册</a> 。</div>
</div>

<div class="itnews c_ad_block" id="itnews">
<a href="http://news.cnblogs.com/" target="_top">IT新闻</a>:<br>

          ·  <a href="http://news.cnblogs.com/n/55140/" target="_top">《星际争霸2》"刀锋女王"重金属主题音乐视频</a><br>
	
          ·  <a href="http://news.cnblogs.com/n/55139/" target="_top">中国站长最郁闷的几件事情</a><br>
	
          ·  <a href="http://news.cnblogs.com/n/55138/" target="_top">苹果Vs谷歌：手机领域谁怕谁？</a><br>
	
          ·  <a href="http://news.cnblogs.com/n/55137/" target="_top">摩托罗拉：豪赌Android的希望与隐忧</a><br>
	
          ·  <a href="http://news.cnblogs.com/n/55136/" target="_top">分众新坐标：楼宇是过去 未来在手机和互联网</a><br>
	
</div>


<div id="ad_under_comment" class="c_ad_block">
<a href="http://a4.yeshj.com/rd/34138/" target="_top">每天10分钟，轻松学英语</a><br><br>
<div id="google_ads_div_cnblogs_blogpost_bottom1">
<iframe src="index_1.html" style="border: 0pt none ;" marginheight="0" marginwidth="0" name="google_ads_iframe_cnblogs_blogpost_bottom1" id="google_ads_iframe_cnblogs_blogpost_bottom1" scrolling="no" frameborder="0" height="60" width="468"></iframe></div>

<br>
<div id="google_ads_div_cnblogs_blogpost_bottom">
<iframe src="index_2.html" style="border: 0pt none ;" marginheight="0" marginwidth="0" name="google_ads_iframe_cnblogs_blogpost_bottom" id="google_ads_iframe_cnblogs_blogpost_bottom" scrolling="no" frameborder="0" height="250" width="300"></iframe></div>

</div>
<div id="site_nav_under" class="c_ad_block">
网站导航：<br>
<a href="http://www.cnblogs.com/" target="_top">博客园首页</a> &nbsp;<a href="http://news.cnblogs.com/" target="_top">IT新闻</a> &nbsp;<a href="http://home.cnblogs.com/" target="_top">个人主页</a> &nbsp;<a href="http://home.cnblogs.com/ing/" target="_top">闪存</a> &nbsp;<a href="http://job.cnblogs.com/" target="_top">程序员招聘</a> &nbsp;<a href="http://space.cnblogs.com/" target="_top">社区</a> &nbsp;<a href="http://space.cnblogs.com/q/" target="_top">博问</a> &nbsp;<a href="http://wz.cnblogs.com/" target="_top">网摘</a>
</div>
<div id="ad_under_comment2" class="c_ad_block">
<a href="http://www.china-pub.com/STATIC07/0912/zh_ndcx_091212.asp" target="_top"><img src="chinapub_20091222.jpg" alt="" style="border: 0px none ;"></a><br>
<a href="http://www.china-pub.com/itbook/" target="_top">China-pub 计算机图书网上专卖店！6.5万品种2-8折！</a><br>
<a href="http://www.china-pub.com/static07/0901/zh_jueba_090121.asp" target="_top">China-Pub 计算机绝版图书按需印刷服务</a><br>
</div>



</div>


				
	</div><!--end: topicList 文章列表容器-->

	<div id="sideBar">
		<div id="sideBarMain">
			
<!--done-->
<div class="newsItem">
<h3 class="catListTitle">公告</h3>
	<div id="profile_block"><a href="http://home.cnblogs.com/i-miss-you/" target="_top">我的主页</a>&nbsp; <a href="http://home.cnblogs.com/i-miss-you/detail/" target="_top">个人资料</a><br><a href="http://home.cnblogs.com/i-miss-you/ing/" target="_top">我的闪存</a>&nbsp; <a href="http://space.cnblogs.com/msg/send/%e8%bf%b7%e5%a4%b1%e8%b7%af%e7%81%af" target="_top">发短消息</a></div>
</div>

			<div id="calendar"><table id="Calendar1_entryCal" class="Cal" title="Calendar" border="0" cellpadding="0" cellspacing="0">
	<tbody><tr><td colspan="7"><table class="CalTitle" border="0" cellspacing="0">
		<tbody><tr><td class="CalNextPrev"><a href="javascript:__doPostBack('Calendar1$entryCal','V3622')" style="color: Black;" title="Go to the previous month">&lt;</a></td><td align="center">2010年1月</td><td class="CalNextPrev" align="right"><a href="javascript:__doPostBack('Calendar1$entryCal','V3684')" style="color: Black;" title="Go to the next month">&gt;</a></td></tr>
	</tbody></table></td></tr><tr><th class="CalDayHeader" abbr="日" scope="col" align="center">日</th><th class="CalDayHeader" abbr="一" scope="col" align="center">一</th><th class="CalDayHeader" abbr="二" scope="col" align="center">二</th><th class="CalDayHeader" abbr="三" scope="col" align="center">三</th><th class="CalDayHeader" abbr="四" scope="col" align="center">四</th><th class="CalDayHeader" abbr="五" scope="col" align="center">五</th><th class="CalDayHeader" abbr="六" scope="col" align="center">六</th></tr><tr><td class="CalOtherMonthDay" align="center">27</td><td class="CalOtherMonthDay" align="center">28</td><td class="CalOtherMonthDay" align="center">29</td><td class="CalOtherMonthDay" align="center">30</td><td class="CalOtherMonthDay" align="center">31</td><td align="center">1</td><td class="CalWeekendDay" align="center">2</td></tr><tr><td class="CalWeekendDay" align="center">3</td><td align="center">4</td><td align="center">5</td><td align="center">6</td><td align="center">7</td><td align="center">8</td><td class="CalWeekendDay" align="center">9</td></tr><tr><td class="CalWeekendDay" align="center">10</td><td align="center">11</td><td align="center">12</td><td align="center">13</td><td align="center">14</td><td align="center">15</td><td class="CalWeekendDay" align="center">16</td></tr><tr><td class="CalTodayDay" align="center">17</td><td align="center">18</td><td align="center">19</td><td align="center">20</td><td align="center">21</td><td align="center">22</td><td class="CalWeekendDay" align="center">23</td></tr><tr><td class="CalWeekendDay" align="center">24</td><td align="center">25</td><td align="center">26</td><td align="center">27</td><td align="center">28</td><td align="center">29</td><td class="CalWeekendDay" align="center">30</td></tr><tr><td class="CalWeekendDay" align="center">31</td><td class="CalOtherMonthDay" align="center">1</td><td class="CalOtherMonthDay" align="center">2</td><td class="CalOtherMonthDay" align="center">3</td><td class="CalOtherMonthDay" align="center">4</td><td class="CalOtherMonthDay" align="center">5</td><td class="CalOtherMonthDay" align="center">6</td></tr>
</tbody></table>
</div>
			
			<div id="leftcontentcontainer">
				
<div id="side_ing_block">
<h3 class="catListTitle">最新闪存</h3>
<ul><li>又迟到了= = <a href="http://home.cnblogs.com/ing/64649/" class="ing_lnk">11-20 10:15</a></li><li><a href="http://home.cnblogs.com/i-miss-you/ing/" class="ing_lnk">更多闪存...</a></li></ul>
</div>



<div class="mySearch">
<h3 class="catListTitle">搜索</h3>
<div class="div_my_zzk"><input value="" id="q" onkeydown="return zzk_go_enter(event);" class="input_my_zzk" type="text">&nbsp;<input onclick="zzk_go()" value="找找看" id="btnZzk" class="btn_my_zzk" type="button"></div>
</div>
<div class="catListLink">
<h3 class="catListTitle">常用链接</h3>
<ul>

		<li><a id="SingleColumn1_ctl02_rptMainLinks_ctl00_lnkLinkItem" href="http://www.cnblogs.com/i-miss-you/MyPosts.html">我的随笔</a></li>
	
		<li><a id="SingleColumn1_ctl02_rptMainLinks_ctl01_lnkLinkItem" href="http://home.cnblogs.com/i-miss-you/">我的空间</a></li>
	
		<li><a id="SingleColumn1_ctl02_rptMainLinks_ctl02_lnkLinkItem" href="http://space.cnblogs.com/msg/recent">我的短信</a></li>
	
		<li><a id="SingleColumn1_ctl02_rptMainLinks_ctl03_lnkLinkItem" href="http://www.cnblogs.com/i-miss-you/MyComments.html">我的评论</a></li>
	
<li><a id="itemListLink" onclick="this.blur();WarpClass('itemListLink', 'itemListLin_con');return false;" href="#">更多链接</a></li>
</ul>
<div id="itemListLin_con" style="display: none;">
<ul>

		<li><a id="SingleColumn1_ctl02_repeaterLinks_ctl00_lnkLinkItem" title="我发表过评论的随笔" href="http://www.cnblogs.com/i-miss-you/OtherPosts.html">我的参与</a></li>
	
		<li><a id="SingleColumn1_ctl02_repeaterLinks_ctl01_lnkLinkItem" href="http://www.cnblogs.com/i-miss-you/MyNews.html">我的新闻</a></li>
	
		<li><a id="SingleColumn1_ctl02_repeaterLinks_ctl02_lnkLinkItem" href="http://www.cnblogs.com/i-miss-you/RecentComments.html">最新评论</a></li>
	
		<li><a id="SingleColumn1_ctl02_repeaterLinks_ctl03_lnkLinkItem" href="http://www.cnblogs.com/i-miss-you/tag/">我的标签</a></li>
	
</ul>
</div>
</div>
<div class="catListPostArchive">
<h3 class="catListTitle">随笔档案<span style="font-size: 11px; font-weight: normal;">(1)</span></h3>

<ul>

<li><a id="SingleColumn1_Categories_CatList_ctl00_LinkList_ctl01_Link" href="http://www.cnblogs.com/i-miss-you/archive/2009/11.html">2009年11月 (1)</a> </li>

</ul>

</div>

<div class="catListArticleArchive">
<h3 class="catListTitle">文章档案<span style="font-size: 11px; font-weight: normal;">(1)</span></h3>

<ul>

<li><a id="SingleColumn1_Categories_CatList_ctl01_LinkList_ctl01_Link" href="http://www.cnblogs.com/i-miss-you/archives/2009/11.html">2009年11月 (1)</a> </li>

</ul>

</div>



<div class="catListBlogRank">
<h3 class="catListTitle">积分与排名</h3>
<ul>
	<li class="liScore">
		积分 -	215
	</li><li class="liRank">
		排名 -	22978
	</li>
</ul>
</div>



<div class="catListComment">
<h3 class="catListTitle">最新评论<a id="SingleColumn1__21b7e98_RSSHyperlink1" title="RSS订阅最最新评论" href="http://www.cnblogs.com/i-miss-you/CommentsRSS.aspx"><img title="RSS订阅最最新评论" src="xml.gif" style="border-width: 0px;"></a></h3>

	
			<div class="divRecentCommentAticle">
				<a id="SingleColumn1__21b7e98_CommentList_ctl00_Hyperlink2" class="listitem" href="http://www.cnblogs.com/i-miss-you/articles/1606976.html#1724521">1.&nbsp;Re:Spring.net快速上手指南</a>
			</div>
			<div class="divRecentComment">
				这文章可以发首页嘛，给我扫扫盲:) (陛下)
			</div>		
		
</div>

			</div>
			
		</div><!--end: sideBarMain -->
		<div class="clear"></div>
	</div><!--end: sideBar 侧边栏容器 -->
</div><!--end: home 自定义的最大容器 -->
		<div class="clear"></div>
<div id="footer">
	
<!--done-->
Copyright ©2010 迷失路灯
</div><!--end: footer -->
</center>
    </form>
	
	
      
    
    
    
</body>
</html>
